home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_077 / quest / qlist.d < prev    next >
Text File  |  1992-05-06  |  5KB  |  285 lines

  1. /*
  2.  * qlist.d - list handling utilities for Quest.
  3.  */
  4.  
  5. type
  6.     Id_t = ulong,
  7.  
  8.     IdList_t = struct {
  9.         *IdList_t il_next;
  10.         Id_t il_this;
  11.     },
  12.  
  13.     PropList_t = struct {
  14.         *PropList_t pl_next;
  15.         Id_t pl_id;
  16.         Id_t pl_property;
  17.     },
  18.  
  19.     AttrList_t = struct {
  20.         *AttrList_t al_next;
  21.         Id_t al_id;
  22.         Id_t al_attribute;
  23.         Id_t al_value;
  24.     };
  25.  
  26. Id_t
  27.     ID_NULL = 0;
  28.  
  29. Id_t NextId;                    /* next available id */
  30. *PropList_t PropList;           /* list of properties */
  31. *AttrList_t AttrList;           /* list of attribute-value pairs */
  32.  
  33. /*
  34.  * lInit - initialize list processing.
  35.  */
  36.  
  37. proc lInit()void:
  38.  
  39.     NextId := ID_NULL;
  40.     PropList := nil;
  41.     AttrList := nil;
  42. corp;
  43.  
  44. /*
  45.  * lFree - free a list for the user.
  46.  */
  47.  
  48. proc lFree(*IdList_t il)void:
  49.     *IdList_t ilt;
  50.  
  51.     while il ~= nil do
  52.         ilt := il;
  53.         il := il*.il_next;
  54.         free(ilt);
  55.     od;
  56. corp;
  57.  
  58. /*
  59.  * lTerm - clean up after list processing.
  60.  */
  61.  
  62. proc lTerm()void:
  63.     *PropList_t pl;
  64.     *AttrList_t al;
  65.  
  66.     while PropList ~= nil do
  67.         pl := PropList;
  68.         PropList := pl*.pl_next;
  69.         free(pl);
  70.     od;
  71.     while AttrList ~= nil do
  72.         al := AttrList;
  73.         AttrList := al*.al_next;
  74.         free(al);
  75.     od;
  76. corp;
  77.  
  78. /*
  79.  * getId - return the next unique id.
  80.  */
  81.  
  82. proc getId()Id_t:
  83.  
  84.     NextId := NextId + 1;
  85.     NextId
  86. corp;
  87.  
  88. /*
  89.  * lAdd - add an element to the front of a list.
  90.  */
  91.  
  92. proc lAdd(**IdList_t p; Id_t val)void:
  93.     *IdList_t il;
  94.  
  95.     il := new(IdList_t);
  96.     il*.il_next := p*;
  97.     il*.il_this := val;
  98.     p* := il;
  99. corp;
  100.  
  101. /*
  102.  * lAppend - append an element to the end of a list.
  103.  */
  104.  
  105. proc lAppend(**IdList_t p; Id_t val)void:
  106.     *IdList_t il;
  107.  
  108.     while p* ~= nil do
  109.         p := &p**.il_next;
  110.     od;
  111.     il := new(IdList_t);
  112.     il*.il_next := nil;
  113.     il*.il_this := val;
  114.     p* := il;
  115. corp;
  116.  
  117. /*
  118.  * lDelete - delete an element from a list.
  119.  */
  120.  
  121. proc lDelete(**IdList_t p; Id_t val)void:
  122.     *IdList_t il;
  123.  
  124.     while p* ~= nil and p**.il_this ~= val do
  125.         p := &p**.il_next;
  126.     od;
  127.     if p* ~= nil then
  128.         il := p*;
  129.         p* := il*.il_next;
  130.         free(il);
  131.     fi;
  132. corp;
  133.  
  134. /*
  135.  * lGet - get the nth entry on a list.
  136.  */
  137.  
  138. proc lGet(*IdList_t p; ulong n)Id_t:
  139.  
  140.     while
  141.         n := n - 1;
  142.         n ~= 0 and p ~= nil
  143.     do
  144.         p := p*.il_next;
  145.     od;
  146.     if p = nil then
  147.         ID_NULL
  148.     else
  149.         p*.il_this
  150.     fi
  151. corp;
  152.  
  153. /*
  154.  * lIn - say if a value is in a list.
  155.  */
  156.  
  157. proc lIn(*IdList_t il; Id_t n)bool:
  158.  
  159.     while il ~= nil and il*.il_this ~= n do
  160.         il := il*.il_next;
  161.     od;
  162.     il ~= nil
  163. corp;
  164.  
  165. /*
  166.  * _pFind - find the list element for a given id-property pair.
  167.  */
  168.  
  169. proc _pFind(Id_t id, prop)**PropList_t:
  170.     **PropList_t ppl;
  171.  
  172.     ppl := &PropList;
  173.     while ppl* ~= nil and (ppl**.pl_id ~= id or ppl**.pl_property ~= prop) do
  174.         ppl := &ppl**.pl_next;
  175.     od;
  176.     ppl
  177. corp;
  178.  
  179. /*
  180.  * putProp - associate a property with an id.
  181.  */
  182.  
  183. proc putProp(Id_t id, prop)void:
  184.     *PropList_t pl;
  185.  
  186.     if _pFind(id, prop)* = nil then
  187.         pl := new(PropList_t);
  188.         pl*.pl_next := PropList;
  189.         pl*.pl_id := id;
  190.         pl*.pl_property := prop;
  191.         PropList := pl;
  192.     fi;
  193. corp;
  194.  
  195. /*
  196.  * getProp - return 'true' if a property is associated with an id.
  197.  */
  198.  
  199. proc getProp(Id_t id, prop)bool:
  200.  
  201.     _pFind(id, prop)* ~= nil
  202. corp;
  203.  
  204. /*
  205.  * delProp - delete the given property from the given id.
  206.  */
  207.  
  208. proc delProp(Id_t id, prop)void:
  209.     **PropList_t ppl;
  210.     *PropList_t pl;
  211.  
  212.     ppl := _pFind(id, prop);
  213.     if ppl* ~= nil then
  214.         pl := ppl*;
  215.         ppl* := pl*.pl_next;
  216.         free(pl);
  217.     fi;
  218. corp;
  219.  
  220. /*
  221.  * _aFind - find the list element for a given id-attribute pair.
  222.  */
  223.  
  224. proc _aFind(Id_t id, attr)**AttrList_t:
  225.     **AttrList_t pal;
  226.  
  227.     pal := &AttrList;
  228.     while pal* ~= nil and (pal**.al_id ~= id or pal**.al_attribute~=attr) do
  229.         pal := &pal**.al_next;
  230.     od;
  231.     pal
  232. corp;
  233.  
  234. /*
  235.  * putAttr - associate an attribute-value with an id.
  236.  */
  237.  
  238. proc putAttr(Id_t id, attr, val)void:
  239.     **AttrList_t pal;
  240.     *AttrList_t al;
  241.  
  242.     pal := _aFind(id, attr);
  243.     if pal* = nil then
  244.         al := new(AttrList_t);
  245.         al*.al_next := AttrList;
  246.         al*.al_id := id;
  247.         al*.al_attribute := attr;
  248.         AttrList := al;
  249.     else
  250.         al := pal*;
  251.     fi;
  252.     al*.al_value := val;
  253. corp;
  254.  
  255. /*
  256.  * getAttr - return the value of an attribute associated with an id.
  257.  */
  258.  
  259. proc getAttr(Id_t id, attr)Id_t:
  260.     **AttrList_t pal;
  261.  
  262.     pal := _aFind(id, attr);
  263.     if pal* = nil then
  264.         ID_NULL
  265.     else
  266.         pal**.al_value
  267.     fi
  268. corp;
  269.  
  270. /*
  271.  * delAttr - delete the given attribute from the given id.
  272.  */
  273.  
  274. proc delAttr(Id_t id, attr)void:
  275.     **AttrList_t pal;
  276.     *AttrList_t al;
  277.  
  278.     pal := _aFind(id, attr);
  279.     if pal* ~= nil then
  280.         al := pal*;
  281.         pal* := al*.al_next;
  282.         free(al);
  283.     fi;
  284. corp;
  285.